<template>
  <el-row class="addOrEditM">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="姓名" prop="name" v-if="dtype=='add'||dtype=='edit'">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="请输入姓名"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="手机号码" prop="telephone" v-if="dtype=='add'||dtype=='edit'">
        <el-col :span="6">
          <el-input v-model="ruleForm.telephone" placeholder="请输入手机号码"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="dtype=='add'||dtype=='reset'">
        <el-col :span="6">
          <el-input v-model="ruleForm.password" :placeholder="dtype=='add'?'请输入管理员密码':'请输入新密码'"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item>
        <el-button type="primary" size="small" @click="submitForm('ruleForm')">{{dtype=="add"?"添加":(dtype=="reset"?"重置":"修改")}}</el-button>
        <el-button size="small" @click="rePrev()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>

<script>
  export default{
    data(){
      return {
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '请输入手机号码', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        }
      }
    },
    props:{
      ruleForm:{
        type:Object,
        default(){
          return {
            name: '',
            telephone: '',
            password:'123456'
          }
        }
      },
      dtype:{
        type:String,
        default:"add"
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "/mng/"+(this.dtype=="add"?"add":(this.dtype=="reset"?"resetPass":"modify"));
            let p = {
              communityId:this.ruleForm.communityId,		//小区id
              mngId:this.ruleForm.mngId,            //管理员id
              realName:this.ruleForm.name,
              phoneNum:this.ruleForm.telephone,
              password:this.ruleForm.password,
              newPass:this.ruleForm.password
            };
            this.$http
              .fetch(url, p)
              .then(res => {
                this.$emit("rePrev",true);
            })
            .catch(err => {});
          } else {
            return false;
          }
        });
      },
      rePrev() {
        this.$emit("rePrev");
      }
    }
  }
</script>

<style>
</style>
